#include <List.h>
#include <kstdio.h>

List::List(){
	//kprintf("LinkConstructor\n");
	head.next = &head;
	head.prev = &head;
	sz = 0;
}

void List::init(){
	head.next = &head;
	head.prev = &head;
	sz = 0;
}


Link * List::getHead(){
	return &head;
}

void List::insert(Link *obj, direction drt, Link *pred_obj = 0){
	if(!pred_obj) pred_obj = &head;
	switch(drt){
		case AFTER:
			obj->next = pred_obj->next;
			obj->prev = pred_obj;
			pred_obj->next = obj;
			obj->next->prev = obj;
			break;
		case BEFOR:
			obj->next = pred_obj;
			obj->prev = pred_obj->prev;
			pred_obj->prev = obj;
			obj->prev->next = obj;
			break;
	}
	++sz;
}

void List::insBegin(Link *obj){
	insert(obj, AFTER);
}

void List::insEnd(Link *obj){
	insert(obj, BEFOR);
}

void List::push(Link *obj){
	insBegin(obj);
}


Link * List::remove(Link *obj){
	if(obj != &head){
		obj->prev->next = obj->next;
		obj->next->prev = obj->prev;
		--sz;
		return obj->next;
	}
}


Link * List::pop(){
	Link *lnk;
	if(!isEmpty() && getSize()){
		lnk = head.next;
		remove(lnk);
		return lnk;
	}
	return 0;
}



